package foundation.pcmUtil;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.jdom2.Element;
import org.jdom2.JDOMException;

import foundation.fileUtil.XMLFileUtil;

public class ResEnvirExtFileHelper {
	private XMLFileUtil xmlHelper = null;
	private String resEnvirFileName = null;

	public ResEnvirExtFileHelper() {

	}

	public ResEnvirExtFileHelper(String resEnvirFileName) {
		this.resEnvirFileName = resEnvirFileName;
		File resEnvirFile = new File(resEnvirFileName);
		xmlHelper = new XMLFileUtil(resEnvirFile);
	}

	public List<Element> readNode(String pathName) throws JDOMException {
		return xmlHelper.readNode(pathName);
	}

	public List<Element> readNode(String pathName, String prefix, String url) throws JDOMException {
		return xmlHelper.readNode(pathName, prefix, url);
	}

	public synchronized void write() throws FileNotFoundException, IOException {
		xmlHelper.write(resEnvirFileName);
	}

	public void close() {
		xmlHelper.close(resEnvirFileName);
	}
	
	/**
	 * 更改某个server的CPU处理速度
	 * @param entityName
	 * @param spd
	 */
	public void mdfyenvirExt(List<StringBuffer> codes){
//		String path = "/resourceModelExtension/"+"resourceContainer[@name='" + "FrontEnd" + "']/";
		Element procRate;
		try {
//			procRate = xmlHelper.readNode(path, "resourceModelExtension",
//					"http://www.modaclouds.eu/xsd/2013/6/resource-model-extension").get(0);
			procRate =xmlHelper.readNode("").get(0);
			Element cpuProcRate = procRate.getChildren().get(2);
			cpuProcRate.setAttribute("specification", String.valueOf(""));
			// 将要更改的数据写到更改后文件
			write();
		} catch (JDOMException | IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
			
	}
	
	public static void main(String[] args){
		List<StringBuffer> li = new ArrayList<>();
		StringBuffer stringBuffer = new StringBuffer();
		stringBuffer.append(1);
		li.add(stringBuffer);
		
		ResEnvirExtFileHelper resEnvirExtFileHelper1 = new ResEnvirExtFileHelper("C:\\Users\\Administrator\\git\\SAOpt4CA\\SAOpt4CA\\demo\\mic\\runFiles\\resultFiles\\resExt.resourceenvironment");
		resEnvirExtFileHelper1.mdfyenvirExt(li);
	}
	
	public XMLFileUtil getXmlHelper() {
		return xmlHelper;
	}

	public void setXmlHelper(XMLFileUtil xmlHelper) {
		this.xmlHelper = xmlHelper;
	}

	public String getResEnvirFileName() {
		return resEnvirFileName;
	}

	public void setResEnvirFileName(String resEnvirFileName) {
		this.resEnvirFileName = resEnvirFileName;
	}

}
